﻿using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace DLL
{
    public class LogHelper
    {
        private static bool isinit = false;
        private static bool LogComplementEnable = false;
        private static bool LogDubugEnable = false;
        private static bool LogErrorEnable = false;
        private static bool LogExceptionEnable = false;
        private static bool LogFatalEnabled = false;
        private static Logger logger = LogManager.GetCurrentClassLogger();
        private static bool LogInfoEnable = false;

        static LogHelper()
        {
            if (!isinit)
            {
                isinit = true;
                SetConfig();
            }
        }

        private static string BuildMessage(string info)
        {
            return BuildMessage(info, null);
        }

        private static string BuildMessage(string info, Exception ex)
        {
            StringBuilder builder = new StringBuilder();
            //HttpContext current = HttpContext.Current;
            builder.AppendFormat("Time:{0}-{1}\r\n", System.DateTime.Now, info);
            //if (current != null)
            //{
            //    builder.AppendFormat("Url:{0}\r\n", current.Request.Url);
            //    if (null != current.Request.UrlReferrer)
            //    {
            //        builder.AppendFormat("UrlReferrer:{0}\r\n", current.Request.UrlReferrer);
            //    }
            //    builder.AppendFormat("UserHostAddress:{0}\r\n", current.Request.UserHostAddress);
            //}
            if (ex != null)
            {
                builder.AppendFormat("Exception:{0}\r\n", ex.ToString());
            }
            builder.AppendLine();
            return builder.ToString();
        }

        public static void SetConfig()
        {
            LogInfoEnable = logger.IsInfoEnabled;
            LogErrorEnable = logger.IsErrorEnabled;
            LogExceptionEnable = logger.IsErrorEnabled;
            LogComplementEnable = logger.IsTraceEnabled;
            LogFatalEnabled = logger.IsFatalEnabled;
            LogDubugEnable = logger.IsDebugEnabled;
        }
        /// <summary>
        /// 最常见的记录信息，一般用于普通输出；
        /// </summary>
        /// <param name="info"></param>
        public static void WriteComplement(string info)
        {
            if (LogComplementEnable)
            {
                logger.Trace(BuildMessage(info));
            }
        }
        /// <summary>
        /// 最常见的记录信息，一般用于普通输出；
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ex"></param>
        public static void WriteComplement(string info, Exception ex)
        {
            if (LogComplementEnable)
            {
                logger.Trace(BuildMessage(info, ex));
            }
        }
        /// <summary>
        /// 同样是记录信息，不过出现的频率要不Trace少一些，一般用来调试程序；
        /// </summary>
        /// <param name="info"></param>
        public static void WriteDebug(string info)
        {
            if (LogDubugEnable)
            {
                logger.Debug(BuildMessage(info));
            }
        }
        /// <summary>
        /// 错误信息
        /// </summary>
        /// <param name="info"></param>
        public static void WriteError(string info)
        {
            if (LogErrorEnable)
            {
                logger.Error(BuildMessage(info));
            }
        }
        /// <summary>
        /// 错误信息，包含错误消息日志
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ex"></param>
        public static void WriteException(string info, Exception ex)
        {
            if (LogExceptionEnable)
            {
                logger.Error(BuildMessage(info, ex));
            }
        }
        /// <summary>
        /// 错误信息，包含错误消息日志
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ex"></param>
        public static void WriteException(string info)
        {
            if (LogExceptionEnable)
            {
                logger.Error(BuildMessage(info));
            }
        }
        /// <summary>
        /// 致命异常信息。一般来讲，发生致命异常之后程序将无法继续执行。
        /// </summary>
        /// <param name="info"></param>
        public static void WriteFatal(string info)
        {
            if (LogErrorEnable)
            {
                logger.Fatal(BuildMessage(info));
            }
        }
        /// <summary>
        /// 信息类型的消息
        /// </summary>
        /// <param name="info"></param>
        public static void WriteInfo(string info)
        {
            if (LogInfoEnable)
            {
                logger.Info(BuildMessage(info));
            }
        }


    }
}
